<?php
require('header.php');
require("settings.php");

if(isset($_SESSION ['s_UserName'])){
	
	$contador = 1; //This variable is to be putting the positions in the ranking.
	$LostGames = 0; //This variable is calculated games lost.
	
	$dbc = mysql_connect($server, $username, $password);
	mysql_select_db('sjoconnect4', $dbc);
	
	$query = 'SELECT * FROM Players order by WonGames desc'; //Here you select the table and sorted by players who have more wins.
	if ($r = mysql_query($query, $dbc)) { //Here we begin to write a table of statistics.
		
		//In this echo write the title of the columns.
		echo <<<_HTML
			<table border="1">
			<tr>
			<td>Player Ranking</td>
			<td>UserName</td>
			<td>WonGames</td>
			<td>LostGames</td>
			</tr>
_HTML;
			
		while ($row = mysql_fetch_array($r)) { //With this loop we write the rows of players their position in the ranking,their wins and losses.
			
			if($row['PlayedGames'] > 0){ //With this if we do that players who are registered and have not won any game not leave the table.
			
				$LostGames = $row['PlayedGames'] - $row['WonGames'];//Here we calculate subtracting game losing games won to games played.
				
				//With this echo player position write your username and wins and losses.
				echo '<tr><td>'.$contador.'</td><td>'.$row['UserName'].'</td><td>'.$row['WonGames'].'</td><td>'.$LostGames.'</td></tr>';
				$contador++;
			}
		}
		
		echo '</table>';
	}
	
	mysql_close($dbc);
}
else{	
	echo "You should be a registered user to see this page";
}

require('footer.php');
?>
